package net.sourceforge.peers.sip.syntaxencoding;

import net.sourceforge.peers.sip.RFC3261;

/**
 *
 * @author Mr.Yan
 */
public class NameAddress {

    /**
     * 提供<sip:104@124.223.170.227>到sip:104@124.223.170.227的转化
     */
    public static String nameAddressToUri(String nameAddress) {
        int leftPos = nameAddress.indexOf(RFC3261.LEFT_ANGLE_BRACKET);
        int rightPos = nameAddress.indexOf(RFC3261.RIGHT_ANGLE_BRACKET);
        if (leftPos < 0 || rightPos < 0) {
            return nameAddress;
        }
        return nameAddress.substring(leftPos + 1, rightPos);
    }
    
    protected String addrSpec;
    protected String displayName;

    public NameAddress(String addrSpec) {
        super();
        this.addrSpec = addrSpec;
    }

    public NameAddress(String addrSpec, String displayName) {
        super();
        this.addrSpec = addrSpec;
        this.displayName = displayName;
    }
    
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        if (displayName != null) {
            buf.append(displayName);
            buf.append(' ');
        }
        buf.append(RFC3261.LEFT_ANGLE_BRACKET);
        buf.append(addrSpec);
        buf.append(RFC3261.RIGHT_ANGLE_BRACKET);
        return buf.toString();
    }

    public String getAddrSpec() {
        return addrSpec;
    }
    
}
